import java.util.*;

class InCop implements Comparator<Integer>{

    @Override
    public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
    }
}
public class TestDemo {
    public static int[] func(int[] array1,int[] array2) {  //两个有序数组合并
        int s1 = 0;
        int s2 = 0;
        int e1 = array1.length-1;
        int e2 = array2.length-1;
        int[] newarray = new int[array1.length+array2.length];
        for (int i = 0; i < array1.length+array2.length; i++) {
            if(s1 > e1) {                          //
                newarray[i] = array2[s2];
                s2++;
                continue;
            }
            if(s2 > e2) {
                newarray[i] = array1[s1];
                s1++;
                continue;
            }
            if(array1[s1] <= array2[s2]) {
                newarray[i] = array1[s1];
                s1++;
            }else {
                newarray[i] = array2[s2];
                s2++;
            }
        }
        return newarray;
    }


    public static int[] smallestK(int[] arr, int k) {
        InCop inCop = new InCop();
        PriorityQueue<Integer> minHeap = new PriorityQueue<>(k,inCop);
        for (int x: arr) {
            if(minHeap.size()< k) {
                minHeap.offer(x);
            }else {
                if(minHeap.peek()>x) {
                    minHeap.poll();
                    minHeap.offer(x);
                }else {
                    continue;
                }
            }
        }
        int[] ret = new int[k];
        for (int i = 0; i < k; i++) {
            ret[i] = minHeap.poll();
        }
        return ret;
    }

    public static void main(String[] args) {
        int[] a = {1,11,7,10};
        int[] ret = smallestK(a,3);
        System.out.println(Arrays.toString(ret));
    }
    public static void main1(String[] args) {
        int[] a = {1,6,7,10};
        int[] b = {2,3,4,9};
        int[] c =  func(a,b);
        System.out.println(Arrays.toString(c));
    }
}
